/*LICENSE_BEGIN
 * ******************************************************************
 * Copyright 2006 by the Dainamite Robocup-Team at the
 * DAI-Labor, TU-Berlin (www.dai-labor.de)
 * 
 * Members of Dainamite are:
 * Holger Endert
 * Robert Wetzker
 * Thomas Karbe
 * Axel Heßler
 * Philippe Bütter
 * Felix Brossmann
 * 
 * Licensed under the GNU GPL (General Public License) Free Software 
 * License, Version 2. You may use this file only in compliance with 
 * the License. You may obtain a copy of the License at:
 * 
 * http://www.fsf.org/licensing/licenses/gpl.txt
 * ******************************************************************
LICENSE_END*/
/* Generated By:JavaCC: Do not edit this line. OtherParser.java */
                           // Robocup Message Parser
package robocup.parser9;

// Infotype for this parser
import robocup.component.infotypes.ChangePlayerTypeInfo;
import robocup.component.infotypes.ChangeOpponentTypeInfo;
import robocup.component.infotypes.Info;
import robocup.component.AbstractAgent;
import java.io.StringReader;

/**
 * The Parser for all remeining messages such as score, errors and changed
 * player types. This class is created by the
 * javacc parser generator. Therefore do not change this class directly,
 * but change the OtherParser.jj and regenerate the code afterwards.
 */
public class OtherParser implements OtherParserConstants {

  /**
   * The command to parse.
   */
  String cmd;

  /**
   * A reference to the agent, that contains the parser.
   */
  AbstractAgent rc;

  /**
   * The info-type, which is returned.
   */
  Info cpti;

        /**
	 * Creates a new instance of the parser.
	 * @param - StringReader - reader for accessing strings
	 * @param - AbstractAgent - the agent object containing this parser
	 */
  public OtherParser(StringReader str, AbstractAgent rc) {
        this(str);
        this.rc = rc;
  }

  /**
   * Parses the score info.
   */
  public Info parseScore(String cmd)
        throws ParseException{
      ReInit(new StringReader(cmd));

      try {
        startScore();
      } catch (ParseException e) {
        // System.out.println("Exception during Parsing with Command:");
        // System.out.println("  " + cmd +"\r\n"+ e);
      }
      return null;
  }

  /**
   * Parses the ok info.
   */
  public Info parseOk(String cmd)
        throws ParseException{
      ReInit(new StringReader(cmd));
      this.cmd = cmd;

      try {
        startOk();
      } catch (ParseException e) {
        System.out.println("Exception during Parsing with Command:");
        System.out.println("  " + cmd +"\r\n"+ e);
      }
      return null;
  }

  /**
   * Parses the error info.
   */
  public Info parseError(String cmd)
        throws ParseException{
      ReInit(new StringReader(cmd));
      this.cmd = cmd;

      try {
        startError();
      } catch (ParseException e) {
        System.out.println("Exception during Parsing with Command:");
        System.out.println("  " + cmd +"\r\n"+ e);
      }
      return null;
  }

  /**
   * Parses the change-player-type info.
   * @param the corresponding message
   * @return the corresponding info object.
   */
  public Info parseChangeType(String cmd) throws ParseException {
    ReInit(new StringReader(cmd));
    this.cmd = cmd;

    try {
      startChangeType();
    } catch (ParseException e) {
      System.out.println("Exception during Parsing with Command:");
      System.out.println("  " + cmd +"\r\n"+ e);
    }

    return cpti;
  }

  final public void startScore() throws ParseException {
    jj_consume_token(OB);
    jj_consume_token(CB);
  }

  final public void startOk() throws ParseException {
    label_1:
    while (true) {
      switch (jj_nt.kind) {
      case SEP:
      case QM:
      case NAM:
        ;
        break;
      default:
        jj_la1[0] = jj_gen;
        break label_1;
      }
      switch (jj_nt.kind) {
      case NAM:
        jj_consume_token(NAM);
        break;
      case SEP:
        jj_consume_token(SEP);
        break;
      case QM:
        jj_consume_token(QM);
        break;
      default:
        jj_la1[1] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
                System.out.println("Server accepted: " + cmd);
  }

  final public void startError() throws ParseException {
    label_2:
    while (true) {
      switch (jj_nt.kind) {
      case OB:
      case CB:
      case SEP:
      case QM:
      case NAM:
        ;
        break;
      default:
        jj_la1[2] = jj_gen;
        break label_2;
      }
      switch (jj_nt.kind) {
      case NAM:
        jj_consume_token(NAM);
        break;
      case SEP:
        jj_consume_token(SEP);
        break;
      case QM:
        jj_consume_token(QM);
        break;
      case OB:
        jj_consume_token(OB);
        break;
      case CB:
        jj_consume_token(CB);
        break;
      default:
        jj_la1[3] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    }
                System.out.println("Fehler vom Server: " + rc
                        + "\n\tReceived: " + cmd);
  }

  final public void startChangeType() throws ParseException {
        Token pNum; Token tNum=null;
    pNum = jj_consume_token(NUM);
    switch (jj_nt.kind) {
    case SEP:
      jj_consume_token(SEP);
      tNum = jj_consume_token(NUM);
      break;
    default:
      jj_la1[4] = jj_gen;
      ;
    }
                if (tNum==null) {
                        // opponents exchanged player
                        cpti = new ChangeOpponentTypeInfo(pNum);
                } else {
                        // we exchanged player
                        cpti = new ChangePlayerTypeInfo(pNum, tNum);
                }
  }

  public OtherParserTokenManager token_source;
  SimpleCharStream jj_input_stream;
  public Token token, jj_nt;
  private int jj_gen;
  final private int[] jj_la1 = new int[5];
  static private int[] jj_la1_0;
  static {
      jj_la1_0();
   }
   private static void jj_la1_0() {
      jj_la1_0 = new int[] {0x640,0x640,0x670,0x670,0x40,};
   }

  public OtherParser(java.io.InputStream stream) {
     this(stream, null);
  }
  public OtherParser(java.io.InputStream stream, String encoding) {
    try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source = new OtherParserTokenManager(jj_input_stream);
    token = new Token();
    token.next = jj_nt = token_source.getNextToken();
    jj_gen = 0;
    for (int i = 0; i < 5; i++) jj_la1[i] = -1;
  }

  public void ReInit(java.io.InputStream stream) {
     ReInit(stream);
  }
  public void ReInit(java.io.InputStream stream, String encoding) {
    try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source.ReInit(jj_input_stream);
    token = new Token();
    token.next = jj_nt = token_source.getNextToken();
    jj_gen = 0;
    for (int i = 0; i < 5; i++) jj_la1[i] = -1;
  }

  public OtherParser(java.io.Reader stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new OtherParserTokenManager(jj_input_stream);
    token = new Token();
    token.next = jj_nt = token_source.getNextToken();
    jj_gen = 0;
    for (int i = 0; i < 5; i++) jj_la1[i] = -1;
  }

  public void ReInit(java.io.Reader stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    token.next = jj_nt = token_source.getNextToken();
    jj_gen = 0;
    for (int i = 0; i < 5; i++) jj_la1[i] = -1;
  }

  public OtherParser(OtherParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    token.next = jj_nt = token_source.getNextToken();
    jj_gen = 0;
    for (int i = 0; i < 5; i++) jj_la1[i] = -1;
  }

  public void ReInit(OtherParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    token.next = jj_nt = token_source.getNextToken();
    jj_gen = 0;
    for (int i = 0; i < 5; i++) jj_la1[i] = -1;
  }

  final private Token jj_consume_token(int kind) throws ParseException {
    Token oldToken = token;
    if ((token = jj_nt).next != null) jj_nt = jj_nt.next;
    else jj_nt = jj_nt.next = token_source.getNextToken();
    if (token.kind == kind) {
      jj_gen++;
      return token;
    }
    jj_nt = token;
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }

  final public Token getNextToken() {
    if ((token = jj_nt).next != null) jj_nt = jj_nt.next;
    else jj_nt = jj_nt.next = token_source.getNextToken();
    jj_gen++;
    return token;
  }

  final public Token getToken(int index) {
    Token t = token;
    for (int i = 0; i < index; i++) {
      if (t.next != null) t = t.next;
      else t = t.next = token_source.getNextToken();
    }
    return t;
  }

  private java.util.Vector jj_expentries = new java.util.Vector();
  private int[] jj_expentry;
  private int jj_kind = -1;

  public ParseException generateParseException() {
    jj_expentries.removeAllElements();
    boolean[] la1tokens = new boolean[11];
    for (int i = 0; i < 11; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 5; i++) {
      if (jj_la1[i] == jj_gen) {
        for (int j = 0; j < 32; j++) {
          if ((jj_la1_0[i] & (1<<j)) != 0) {
            la1tokens[j] = true;
          }
        }
      }
    }
    for (int i = 0; i < 11; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.addElement(jj_expentry);
      }
    }
    int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++) {
      exptokseq[i] = (int[])jj_expentries.elementAt(i);
    }
    return new ParseException(token, exptokseq, tokenImage);
  }

  final public void enable_tracing() {
  }

  final public void disable_tracing() {
  }

}
